/**
 * @Author MrLi
 * @Version 1.0
 * @Date 2023-08-11 13:23:09
 */
package com.cl.erp_2301.controller;

import com.cl.erp_2301.Service.InventoryService;
import com.cl.erp_2301.dto.DataDTO;
import com.cl.erp_2301.dto.InventoryDTO;
import com.cl.erp_2301.entity.Goods;
import com.cl.erp_2301.entity.Store;
import com.cl.erp_2301.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v2")
/*/v2/inventory/listByPage*/
public class InventoryCotroller {

    @Resource
    private InventoryService inventoryService;

    @RequestMapping(value = "/inventory/listByPage", method = RequestMethod.GET)
    public Result findAllBy(HttpSession session, InventoryDTO inventoryDTO,
                          int page, int rows) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        String storeName = (String) session.getAttribute("storeName");
        inventoryDTO.setStorename(storeName);
        List<InventoryDTO> storeDtoList = inventoryService.findAllBy(inventoryDTO);
        DataDTO data1 = new DataDTO();
        data1.setRows(storeDtoList);
        data1.setTotal(page1.getTotal());
        return Result.ok(data1);
    }

    /*/inventory/goodsList*/
    @ResponseBody
    @RequestMapping("/inventory/goodsList")
    public Result goodsList() {
        List<Goods> goodsList = inventoryService.goodsList();
        return Result.okRows(goodsList);
    }

    @ResponseBody
    @RequestMapping("/inventory/storeList")
    public Result storeList() {
        List<Store> goodsList = inventoryService.storeList();
        return Result.okRows(goodsList);
    }

    /*/inventory/add*/
    @PostMapping("/inventory/add")
    public Result add1(@RequestParam(value = "goodsuuid", required = false, defaultValue = "") Integer goodsuuid,
                       @RequestParam(value = "storeuuid", required = false, defaultValue = "") Integer storeuuid,
                       @RequestParam(value = "num", required = false, defaultValue = "") Integer num,
                       @RequestParam(value = "type", required = false, defaultValue = "") String type,
                       @RequestParam(value = "remark", required = false, defaultValue = "") String remark,
                       HttpSession session) {
        InventoryDTO inventoryDTO = new InventoryDTO();
        String name = (String) session.getAttribute("name");
        inventoryDTO.setName(name);
        inventoryDTO.setGoodsuuid(goodsuuid);
        inventoryDTO.setStoreuuid(storeuuid);
        inventoryDTO.setType(type);
        inventoryDTO.setNum(num);
        inventoryDTO.setRemark(remark);
        inventoryService.add1(inventoryDTO);
        return Result.ok();
    }

    /*/inventory/selectone*/
    @ResponseBody
    @RequestMapping("/inventory/selectone")
    public Result add2(HttpServletRequest request,
                       @RequestParam("id") Integer uuid) {
        InventoryDTO inventoryDTO = new InventoryDTO();
        inventoryDTO.setUuid(uuid);
        request.getSession().setAttribute("uuid", inventoryDTO.getUuid());
        return Result.ok();
    }

    /*/inventory/update*/
    @PostMapping("/inventory/update")
    public Result updateById(HttpSession session, InventoryDTO inventoryDTO,
                             @RequestParam("goodsuuid") Integer goodsuuid,
                             @RequestParam("storeuuid")Integer storeuuid,
                             @RequestParam("num") Integer num,
                             @RequestParam("type") String type,
                             @RequestParam("remark") String remark) {
        Integer id = (Integer) session.getAttribute("uuid");
        String name = (String) session.getAttribute("name");
        inventoryDTO.setGoodsuuid(goodsuuid);
        inventoryDTO.setStoreuuid(storeuuid);
        inventoryDTO.setNum(num);
        inventoryDTO.setType(type);
        inventoryDTO.setRemark(remark);
        inventoryDTO.setChecktime(new Date());
        inventoryDTO.setChecker(name);
        inventoryDTO.setUuid(id);
        inventoryService.updateById(inventoryDTO);
        System.out.println(inventoryDTO);
        return Result.ok();
    }

    /*@RequestMapping(value = "/inventory/listByPage", method = RequestMethod.GET)*/
    @RequestMapping(value = "/inventory", method = RequestMethod.DELETE)
    public Result deleteById(@RequestParam(value = "id", required = false, defaultValue = "") Integer id) {
        inventoryService.deleteById(id);
        return Result.ok();
    }
}
